package com.lineage.server.model.Instance;

import com.lineage.server.datatables.NPCTalkDataTable;
import com.lineage.server.model.L1AttackPc;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1NpcTalkData;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1War;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_NPCPack;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_RemoveObject;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.timecontroller.server.ServerWarExecutor;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import com.lineage.server.world.WorldWar;
import java.util.ArrayList;
import java.util.Iterator;

public class L1CatapultInstance extends L1NpcInstance {
  private static final long serialVersionUID = 1L;
  
  private int _castle_id;
  
  public L1CatapultInstance(L1Npc template) {
    super(template);
  }
  
  public void onPerceive(L1PcInstance perceivedFrom) {
    if (perceivedFrom == null)
      return; 
    perceivedFrom.addKnownObject((L1Object)this);
    perceivedFrom.sendPackets((ServerBasePacket)new S_NPCPack(this));
  }
  
  public void onTalkAction(L1PcInstance pc) {
    if (pc == null)
      return; 
    int objid = getId();
    L1NpcTalkData talking = NPCTalkDataTable.get().getTemplate(getNpcTemplate().get_npcId());
    String htmlid = null;
    String[] htmldata = null;
    int npcId = getNpcTemplate().get_npcId();
    if (npcId >= 90327 && npcId <= 90330) {
      this._castle_id = 1;
    } else if (npcId >= 90331 && npcId <= 90334) {
      this._castle_id = 4;
    } else if (npcId >= 90335 && npcId <= 90337) {
      this._castle_id = 2;
    } 
    boolean existDefenseClan = false;
    L1Clan clan = WorldClan.get().getClan(pc.getClanname());
    if (clan != null) {
      int clanCastleId = clan.getCastleId();
      if (clanCastleId == this._castle_id)
        existDefenseClan = true; 
    } 
    boolean isProclamation = false;
    Iterator<L1War> iterator = WorldWar.get().getWarList().iterator();
    while (iterator.hasNext()) {
      L1War war = iterator.next();
      if (this._castle_id == war.get_castleId()) {
        isProclamation = war.checkClanInWar(pc.getClanname());
        break;
      } 
    } 
    if (!pc.isGm()) {
      if (clan == null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(2498));
        return;
      } 
      if (!ServerWarExecutor.get().isNowWar(this._castle_id)) {
        if (pc.isCrown() && pc.getId() == clan.getLeaderId()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(3683));
          return;
        } 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(2498));
        return;
      } 
      if (!isProclamation) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(2498));
        return;
      } 
      if (pc.isCrown() && pc.getId() == clan.getLeaderId()) {
        if (existDefenseClan) {
          if (npcId == 90327 || npcId == 90328 || npcId == 90331 || npcId == 90332 || npcId == 90335 || 
            npcId == 90336) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(3681));
            return;
          } 
        } else if (npcId == 90329 || npcId == 90330 || npcId == 90333 || npcId == 90334 || npcId == 90337) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(3682));
          return;
        } 
      } else {
        if (npcId == 90327 || npcId == 90328 || npcId == 90331 || npcId == 90332 || npcId == 90335 || 
          npcId == 90336) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(3681));
          return;
        } 
        if (npcId == 90329 || npcId == 90330 || npcId == 90333 || npcId == 90334 || npcId == 90337) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(3682));
          return;
        } 
      } 
    } 
    switch (npcId) {
      case 90327:
      case 90328:
        htmlid = "ckenta";
        break;
      case 90329:
      case 90330:
        htmlid = "ckentd";
        break;
      case 90331:
      case 90332:
        htmlid = "cgirana";
        break;
      case 90333:
      case 90334:
        htmlid = "cgirand";
        break;
      case 90335:
      case 90336:
        htmlid = "corca";
        break;
      case 90337:
        htmlid = "corcd";
        break;
    } 
    if (htmlid != null) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(objid, htmlid));
    } else if (pc.getLawful() < -1000) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(talking, objid, 2));
    } else {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(talking, objid, 1));
    } 
  }
  
  public void onAction(L1PcInstance pc) {
    if (pc != null && !isDead())
      if (getCurrentHp() > 0) {
        L1AttackPc l1AttackPc = new L1AttackPc(pc, this);
        if (l1AttackPc.calcHit())
          l1AttackPc.calcDamage(); 
        l1AttackPc.action();
        l1AttackPc.commit();
      } else {
        L1AttackPc l1AttackPc = new L1AttackPc(pc, this);
        l1AttackPc.calcHit();
        l1AttackPc.action();
      }  
  }
  
  public void receiveDamage(L1Character attacker, int damage) {
    int npcId = getNpcTemplate().get_npcId();
    if (npcId >= 90327 && npcId <= 90330) {
      this._castle_id = 1;
    } else if (npcId >= 90331 && npcId <= 90334) {
      this._castle_id = 4;
    } else if (npcId >= 90335 && npcId <= 90337) {
      this._castle_id = 2;
    } 
    if (this._castle_id > 0 && ServerWarExecutor.get().isNowWar(this._castle_id))
      if (getCurrentHp() > 0 && !isDead()) {
        if (damage >= 0 && !(attacker instanceof L1EffectInstance))
          setHate(attacker, damage); 
        L1PcInstance pc = null;
        if (attacker instanceof L1PcInstance) {
          pc = (L1PcInstance)attacker;
        } else if (attacker instanceof L1PetInstance) {
          pc = (L1PcInstance)((L1PetInstance)attacker).getMaster();
        } else if (attacker instanceof L1SummonInstance) {
          pc = (L1PcInstance)((L1SummonInstance)attacker).getMaster();
        } 
        if (pc == null)
          return; 
        boolean existDefenseClan = false;
        Iterator<L1Clan> iterator = WorldClan.get().getAllClans().iterator();
        while (iterator.hasNext()) {
          L1Clan clan = iterator.next();
          int clanCastleId = clan.getCastleId();
          if (clanCastleId == this._castle_id) {
            existDefenseClan = true;
            break;
          } 
        } 
        boolean isProclamation = false;
        Iterator<L1War> iterator2 = WorldWar.get().getWarList().iterator();
        while (iterator2.hasNext()) {
          L1War war = iterator2.next();
          if (this._castle_id == war.get_castleId())
            isProclamation = war.checkClanInWar(pc.getClanname()); 
        } 
        if (existDefenseClan && !isProclamation)
          return; 
        int newHp = getCurrentHp() - damage;
        if (newHp <= 0 && !isDead()) {
          setCurrentHp(0);
          setDead(true);
          setStatus(8);
          death();
        } 
        if (newHp > 0)
          setCurrentHp(newHp); 
      } else if (!isDead()) {
        setDead(true);
        setStatus(8);
        death();
      }  
  }
  
  private void death() {
    try {
      setDeathProcessing(true);
      setCurrentHp(0);
      setDead(true);
      setStatus(8);
      getMap().setPassable((Point)getLocation(), true);
      broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(getId(), 8));
    } catch (Exception exception) {}
  }
  
  public void deleteMe() {
    this._destroyed = true;
    if (getInventory() != null)
      getInventory().clearItems(); 
    World.get().removeVisibleObject((L1Object)this);
    World.get().removeObject((L1Object)this);
    ArrayList<L1PcInstance> list = null;
    list = World.get().getRecognizePlayer((L1Object)this);
    Iterator<L1PcInstance> iterator = list.iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      if (pc == null)
        continue; 
      pc.removeKnownObject((L1Object)this);
      pc.sendPackets((ServerBasePacket)new S_RemoveObject((L1Object)this));
    } 
    removeAllKnownObjects();
  }
}
